﻿using System;
using System.Net;
using System.Net.Mail;

namespace METU_CMT.Business
{
    public class Email
    {
        private string MailBody { get; set; }

        private string Adress { get; set; }

        private string Subject { get; set; }

        public string FromName { get; set; }

        public Email(string mailSubject, string mailText, string mailAdress )
        {
            MailBody = mailText;
            Adress = mailAdress;
            Subject = mailSubject;
        }

        #region E-Mail Gonder Methodu
        public void SendEmail()
        {
            var _fromAddress = new MailAddress("metucmt@gmail.com", "METU CMT");
            
            string[] _splittedEmail = Adress.Split(new[] {";"}, StringSplitOptions.RemoveEmptyEntries);
            string _toEmail = _splittedEmail[0];

            var _toAddress = new MailAddress(_toEmail, "");

            var _smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(_fromAddress.Address, "yg_ahnc_2011")
            };

            using (var _message = new MailMessage(_fromAddress, _toAddress)
            {
                Subject = Subject,
                Body = MailBody
            })
            {
                _message.IsBodyHtml = true;

                foreach (string _mailAddress in _splittedEmail)
                {
                    if(!_mailAddress.Equals(_toEmail))
                        _message.Bcc.Add(_mailAddress);
                }


                //TODO: Burayı aç deployda.
                _smtp.Send(_message);
            }

        }
        #endregion

    }
}